# -*- coding: utf-8 -*-

import hashlib
from django.db import models
from django.contrib.auth.models import AbstractUser
from core.models import CoreModel, table_prefix


class User(AbstractUser, CoreModel):
    username = models.CharField(
        unique=True, max_length=150, db_index=True, verbose_name="用户账号"
    )
    email = models.EmailField(
        null=True, blank=True, max_length=255, verbose_name="邮箱"
    )
    mobile = models.CharField(
        null=True, blank=True, max_length=255, verbose_name="电话"
    )
    avatar = models.CharField(
        null=True, blank=True, max_length=255, verbose_name="头像"
    )
    name = models.CharField(max_length=40, verbose_name="姓名")
    gender = models.CharField(max_length=32, default="2", verbose_name="性别")
    post = models.ManyToManyField(
        to="sys.Post", verbose_name="关联岗位", db_constraint=False
    )
    roles = models.ManyToManyField(
        to="sys.Role",
        related_name="user_set",
        verbose_name="关联角色",
        db_constraint=False,
    )
    dept = models.ForeignKey(
        null=True,
        blank=True,
        to="sys.Dept",
        on_delete=models.PROTECT,
        db_constraint=False,
        verbose_name="所属部门",
    )

    class Meta:
        db_table = table_prefix + "system_user"
        verbose_name = "用户管理"
        verbose_name_plural = verbose_name
        ordering = ("-create_time",)


class UserNotification(CoreModel):
    """
    用户通知模型
    """

    NOTIFICATION_TYPES = (
        ("system", "系统通知"),
        ("message", "消息通知"),
        ("warning", "警告通知"),
        ("error", "错误通知"),
    )

    user = models.ForeignKey(
        User,
        related_name="user_notification_set",
        on_delete=models.CASCADE,
        verbose_name="接收用户",
    )
    title = models.CharField(max_length=200, verbose_name="通知标题")
    content = models.TextField(verbose_name="通知内容")
    type = models.CharField(
        max_length=20,
        choices=NOTIFICATION_TYPES,
        default="system",
        verbose_name="通知类型",
    )
    avatar = models.URLField(
        default="https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png",
        verbose_name="头像URL",
    )
    link = models.CharField(
        default="/system/notice", max_length=200, verbose_name="跳转链接"
    )
    is_read = models.BooleanField(default=False, verbose_name="是否已读")

    class Meta:
        db_table = table_prefix + "system_user_notification"
        verbose_name = "用户消息管理"
        verbose_name_plural = verbose_name
        ordering = ("-create_time",)
